﻿package bdz.projects.lavender.doi.utils
{
	import bdz.common.events.GenericEvent;
	import bdz.common.utils.GlobalRender;
	import flash.events.EventDispatcher;
	import flash.utils.*;
	/**
	 * ...
	 * @author ...
	 */
	public class CuePointManager extends EventDispatcher
	{
		private static var instance:CuePointManager;
		private var compareList:Array;
		private var timeprop:String = "rmarkertime";
		
		
		private var lastmatch:String;
		private var preventDuplicateDispatch:Boolean = true;
		function CuePointManager(se:SingletonEnforcer) {
			
		}
		public static function getInstance():CuePointManager {
			if (instance == null) {
				instance = new CuePointManager(new SingletonEnforcer());
				instance.initialize();
			}
			return instance;
		}
		private function getMinSec(tme:Number):Object {
			var secs:* = Math.floor(tme/1000);
			var mins:* = Math.floor(secs/60);
			secs %= 60;
			mins %= 60;
			if(secs < 10){
				secs = "0"+secs;
			}
			if(mins < 10){
				mins = "0"+mins;
			}
			return {secs:secs,mins:mins};
		}
		private function initialize():void {
			compareList = new Array();
		}
		
		private function compareTimeString(n1:Number, n2:Number, bobj:Object):Boolean {
			var timeString1:String = getMinSec(n1).mins + ":" + getMinSec(n1).secs;
			var timeString2:String = getMinSec(n2).mins + ":" + getMinSec(n2).secs;
			
			if(preventDuplicateDispatch){
				if (timeString1 == timeString2) {
					if (!bobj.matched) {
						bobj.matched = true;
						return true;
					}
				}
			}else {
				return timeString1 == timeString2;
			}
			return false;
		}
		public function analyzeTime(n:Number):void {
			
			
			for each(var o:* in compareList) {
				if (compareTimeString(n, o[timeprop], o)) {
					if(o.markertype == "caption"){
						dispatchEvent(new GenericEvent(GenericEvent.MATCHED, o));
					}else {
						dispatchEvent(new GenericEvent(GenericEvent.TRIGGER_MATCHED, o));
					}
					break;
				}
			}
			
			
		}
		public function dispatchClearCues():void {
			dispatchEvent(new GenericEvent("clearCues"));
		}
		public function applyCompareList(arr:Array):void {
			lastmatch = null;
			for each(var o:Object in arr) {
				o.matched = false;
			}
			compareList = arr;
		}
		public function set timeProperty(str:String):void {
			timeprop = str;
		}
		
	}
	
}
/**
 * @private
 * 
 */
internal class SingletonEnforcer{}